home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / unexpand.c < prev    next >
C/C++ Source or Header  |  1990-07-19  |  2KB  |  118 lines

  1. /*  unexpand - convert spaces to tabs    Author: Terrence W. Holm */
  2.  
  3. /*  Usage:  unexpand  [ -a ]  [ file ... ]  */
  4.  
  5. #include <stdio.h>
  6.  
  7. #define  TAB   8
  8.  
  9.  
  10. int column = 0;            /* Current column, retained between files  */
  11. int spaces = 0;            /* Spaces since last tab stop          */
  12. int leading_blank = 1;        /* Only unexpand leading blanks,        */
  13. /* Overruled by -a option                */
  14.  
  15. main(argc, argv)
  16. int argc;
  17. char *argv[];
  18.  
  19. {
  20.   int all = 0;            /* -a flag means unexpand all spaces  */
  21.   int i;
  22.   FILE *f;
  23.  
  24.   if (argc > 1 && argv[1][0] == '-') {
  25.     if (strcmp(argv[1], "-a") == 0)
  26.         all = 1;
  27.     else {
  28.         fprintf(stderr, "Usage:  unexpand  [ -a ]  [ file ... ]\n");
  29.         exit(1);
  30.     }
  31.  
  32.     --argc;
  33.     ++argv;
  34.   }
  35.   if (argc == 1)
  36.     Unexpand(stdin, all);
  37.   else
  38.     for (i = 1; i < argc; ++i) {
  39.         if ((f = fopen(argv[i], "r")) == NULL) {
  40.             perror(argv[i]);
  41.             exit(1);
  42.         }
  43.         Unexpand(f, all);
  44.         fclose(f);
  45.     }
  46.  
  47.  
  48.   /* If there are pending spaces print them.  */
  49.  
  50.   while (spaces > 0) {
  51.     putchar(' ');
  52.     --spaces;
  53.   }
  54.  
  55.   exit(0);
  56. }
  57.  
  58.  
  59. Unexpand(f, all)
  60. FILE *f;
  61. int all;
  62.  
  63. {
  64.   int c;
  65.  
  66.   while ((c = getc(f)) != EOF) {
  67.     if (c == ' ' && (all || leading_blank)) {
  68.         ++column;
  69.         ++spaces;
  70.  
  71.         /* If we have white space up to a tab stop, then output     */
  72.         /* A tab. If only one space is required, use a ' '.     */
  73.  
  74.         if (column % TAB == 0) {
  75.             if (spaces == 1)
  76.                 putchar(' ');
  77.             else
  78.                 putchar('\t');
  79.  
  80.             spaces = 0;
  81.         }
  82.         continue;
  83.     }
  84.  
  85.     /* If a tab character is encountered in the input then        */
  86.     /* Simply echo it. Any accumulated spaces can only be         */
  87.     /* Since the last tab stop, so ignore them.            */
  88.     if (c == '\t') {
  89.         column = (column / TAB + 1) * TAB;
  90.         spaces = 0;
  91.         putchar('\t');
  92.         continue;
  93.     }
  94.  
  95.     /* A non-space character is to be printed. If there   */
  96.     /* Are pending spaces, then print them. There will be */
  97.     /* At most TAB-1 spaces to print.              */
  98.     while (spaces > 0) {
  99.         putchar(' ');
  100.         --spaces;
  101.     }
  102.  
  103.     if (c == '\n' || c == '\r') {
  104.         column = 0;
  105.         leading_blank = 1;
  106.         putchar(c);
  107.         continue;
  108.     }
  109.     if (c == '\b')
  110.         column = column > 0 ? column - 1 : 0;
  111.     else
  112.         ++column;
  113.  
  114.     leading_blank = 0;
  115.     putchar(c);
  116.   }
  117. }
  118.